package day114;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @Auther: MFM
 * @Date: 2021/11/4 - 11 - 04 - 19:58
 * @version: 1.0
 */

public class Cook {
    private boolean flag;
    private int num = 0;
    private String currentThread;
    BlockingQueue<String> queue = new ArrayBlockingQueue<>(1);

    public synchronized void drawFood() throws Exception {
        if (this.flag) {
            if (currentThread.equals(queue.take())) {
                System.out.println("取走" + Thread.currentThread().getName());
                this.flag = false;
                num++;
                switch (Thread.currentThread().getName()) {
                    case "红烧肉":
                        currentThread = "胖子";
                        System.out.println("胖子取走了红烧肉");
                        queue.take();
                        break;
                    case "西兰花":
                        currentThread = "美女";
                        System.out.println("美女取走了西兰花");
                        queue.take();
                        break;
                    case "糖醋里脊":
                        currentThread = "小兽";
                        System.out.println("小兽取走了糖醋里脊");
                        queue.take();
                        break;
                }
                Thread.sleep(1000);
                notifyAll();
            } else {
                wait();
            }

        } else {
            wait();
        }
    }

    public synchronized void disposeFood() throws Exception {

        if (num < 10) {
            if (this.flag){
                wait();
            } else {
                System.out.println("放入" + Thread.currentThread().getName());
                this.flag = false;
                queue.put(Thread.currentThread().getName());
                Thread.sleep(1000);
                notifyAll();
            }
        }
    }

}
